CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


  CS    = 3
  DI    = 2
  CLK   = 1
  DO    = 0

obj

  d     :       "Parallax Serial Terminal"
  sd    :       "SD-MMC_FATEngine"
  num   :       "Simple_Numbers"


var

  long time1, time2, time3, time4
  long big_stack[200]

pub go | str

  'd.start(115_200)
  sd.FATEngineStart(DO, CLK, DI, CS, -1, -1, -1, -1, -1)

  
  cognew(in1(10), @big_stack[0])
  cognew(in2(11), @big_stack[50])
  cognew(in3(12), @big_stack[100])
  cognew(in4(13), @big_stack[150])
  
  
  str := sd.mountPartition(0) ' Mount the default partition 0.  Can be 0 - 3. 
  if sd.partitionError
    d.str(str)
    repeat

    
  str := sd.openFile(sd.newFile(string("test.csv")), "W") ' Create and open text.txt.
  if sd.partitionError
    d.str(str)
    sd.unmountPartition
    repeat
  
  'd.str(string("All started and file opened.", 13))

  dira[16]~
  
  repeat until ina[16] 
    sd.writeString(num.dec(time1))
    sd.writeByte(",")
    sd.writeString(num.dec(time2))
    sd.writeByte(",")
    sd.writeString(num.dec(time3))
    sd.writeByte(",")
    sd.writeString(num.dec(time4))
    sd.writeByte(13)
    pause(10_000)
    
  sd.closeFile
  sd.unmountPartition



pub in1(pin) | cnt1, cnt2

  dira[pin]~

  repeat
    waitpne(0, |< Pin, 0)                                                         ' Wait For Pin To Go HIGH
    cnt1 := cnt                                                                   ' Store Current Counter Value
    waitpeq(0, |< Pin, 0)                                                         ' Wait For Pin To Go LOW 
    cnt2 := cnt 
    time1 := (||(cnt1 - cnt2) / (clkfreq / 1_000_000))                             'do comparison and return time the pin was high
     
pub in2(pin) | cnt1, cnt2

  dira[pin]~

  repeat 
    waitpne(0, |< Pin, 0)                                                         ' Wait For Pin To Go HIGH
    cnt1 := cnt                                                                   ' Store Current Counter Value
    waitpeq(0, |< Pin, 0)                                                         ' Wait For Pin To Go LOW 
    cnt2 := cnt 
    time2 := (||(cnt1 - cnt2) / (clkfreq / 1_000_000))                             'do comparison and return time the pin was high
     
pub in3(pin) | cnt1, cnt2

  dira[pin]~

  repeat
    waitpne(0, |< Pin, 0)                                                         ' Wait For Pin To Go HIGH
    cnt1 := cnt                                                                   ' Store Current Counter Value
    waitpeq(0, |< Pin, 0)                                                         ' Wait For Pin To Go LOW 
    cnt2 := cnt 
    time3 := (||(cnt1 - cnt2) / (clkfreq / 1_000_000))                             'do comparison and return time the pin was high
     
pub in4(pin) | cnt1, cnt2

  dira[pin]~

  repeat
    waitpne(0, |< Pin, 0)                                                         ' Wait For Pin To Go HIGH
    cnt1 := cnt                                                                   ' Store Current Counter Value
    waitpeq(0, |< Pin, 0)                                                         ' Wait For Pin To Go LOW 
    cnt2 := cnt 
    time4 := (||(cnt1 - cnt2) / (clkfreq / 1_000_000))                             'do comparison and return time the pin was high
     



pri Pause(duration)

  'This function pauses program execution for approximately [duration] micro seconds
  'so 1_000_000 would be approximately one second.  Doesnt account for instruction
  'execution time overhead.

  if duration < 381
    duration := 381             'lower bound limit. anything lower than this, we
                                'have to wait until the counter comes back around,
                                'which is MUCH longer than [duration].
  waitcnt(((clkfreq / 1_000_000) * duration) + cnt)